/**
 * 
 */
package com.etrans.web.gps.business.system.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.etrans.web.gps.base.service.BaseService;
import com.etrans.web.gps.business.system.domain.OperatePermission;
import com.etrans.web.gps.business.system.domain.PermissionMenu;
import com.etrans.web.gps.business.system.domain.SysUser;

/**
 * 菜单类
 * @author jw.chen
 *
 */
@Service
public class MenuService extends BaseService{
	
	/**
	 * 获取权限菜单
	 * 
	 * @param userId
	 * @return
	 */
    public  PermissionMenu getPermissionMenuByUserId(SysUser user){
    	//菜单类
    	PermissionMenu permissionMenu = new PermissionMenu();
    	//一级菜单集合
    	List<OperatePermission> level_one = new ArrayList<>();
    	
    	List<String> operate = new ArrayList<>();
    	String OP_TYPE="RESOURCE";
    	Integer index = 0;
    	
    	//获取当前用户有权限的菜单
    	List<OperatePermission> listOperate = selectList("menuMapper.queryUserOperatePermission", user);
    	
    	for(int i=0;i<listOperate.size();i++){
    		//获取一级菜单
    		OperatePermission operatePermission = listOperate.get(i);
    		if(operatePermission != null && 0==operatePermission.getParent_func_id()){
    			level_one.add(listOperate.get(i));
    		}
    	}
    	// 添加一级菜单
    	permissionMenu.setLevel_one(level_one);
    	
    	if(level_one.size()>0){
    		Map<String,List<OperatePermission>> map_level_two = new HashMap<>();
    		for(int i=0;i<level_one.size();i++){
    			//二级菜单集合
    			List<OperatePermission> level_two = new ArrayList<>();
    			//获取一级菜单Id
    			int mId = level_one.get(i).getMenuId();
    			
    			for(int j = 0; j < listOperate.size(); j++){
    				//获取二级菜单
    				OperatePermission operatePermission = listOperate.get(j);
    				//获取对应的父级菜单Id
    				int pId = operatePermission.getMenuPid();
    				if(mId == pId){
    					level_two.add(operatePermission);
    				}
    			}
    			if(level_two.size()>0){
    				map_level_two.put(String.valueOf(mId), level_two);
    			}
    		}
    		// 设置二级菜单
    		permissionMenu.setLevel_two(map_level_two);
    		
    		// 设置操作权限
    		for(int j=index;j<listOperate.size();j++){
    			OperatePermission operatePermission = listOperate.get(j);
    			if(OP_TYPE.equals(operatePermission.getMenuType())){
    				operate.add(operatePermission.getMenuCode());
    			}
    		}
    		permissionMenu.setOperate(operate);
    		
    		// 过滤掉无二级菜单的一级菜单(首页除外)
    		Iterator<OperatePermission> menu_one = level_one.iterator();
            while (menu_one.hasNext()) {
            	OperatePermission op = menu_one.next();
            	if(!"首页".equals(op.getMenuName()) && null==map_level_two.get(String.valueOf(op.getMenuId()))){
            		menu_one.remove();
            	}
            }
    	}
    	return permissionMenu;
    }

    /**
     * 获取报警分析设置菜单
     * @param userId 用户ID
     * @return
     */
	public List<PermissionMenu> loadResportsetMenu() {
		return this.selectList("menuMapper.queryUserAlarmReporset", null);
	}
	
	/**
	 * 获取三级菜单(先判断是否查同等级的)
	 * @param params 参数
	 * @return
	 */
	public List<HashMap<String,Object>> loadThreeMenu(HashMap<String,Object> params) {
		//查岗管理查下一级，其他查同等级
		int count =  this.selectOne("menuMapper.querySameLevel", params);
		if(count == 1){
			return this.selectList("menuMapper.queryThreeMenu", params);
		}else{
			return this.selectList("menuMapper.querySecondMenu", params);
		}
	}
}
